package com.company;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @description:
 * @author: ZhaoYicong
 * @date: Created in 2020/7/10 8:27
 * @version: v1.0
 * @modified By:
 */
public class Jdbcutil {

    //获取链接
    public static Connection getconnection() throws Exception {
        //加载配置文件获取相关信息
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.proterties");
        Properties ps=new Properties();
        ps.load(is);

        String user = ps.getProperty("user");
        String url = ps.getProperty("url");
        String password = ps.getProperty("password");
        String driverClass = ps.getProperty("driverClass");
        //加载驱动
        Class.forName(driverClass);
        //获取链接
        Connection connection = DriverManager.getConnection(url, user, password);

        return connection;
    }

    //利用数据库连接池来获取 链接
    private static DataSource source;
    static {
        try {
            InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.proterties");
            Properties ps=new Properties();
            ps.load(is);
            source = DruidDataSourceFactory.createDataSource(ps);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getconnection2() throws SQLException {

        Connection connection = source.getConnection();
        return connection;
    }




    //增删改的操作
    public static void update(Connection connection,String sql,Object...args) throws Exception {
        //获取数据库链接

        //执行预编译
        PreparedStatement ps = connection.prepareStatement(sql);
        // 开始填充
        for (int i=0;i<args.length;i++){
            ps.setObject(i+1,args[i]);

        }
        //执行
        ps.execute();
        //关闭
        closeResource(null,ps);


    }

    //查询的操作
    public static <T> T select(Connection connection,Class<T> clzz ,String sql,Object ...args) throws Exception, InstantiationException {

        PreparedStatement ps = connection.prepareStatement(sql);

        for (int i = 0; i < args.length; i++) {
            ps.setObject(i+1,args[i]);
        }

        ResultSet rs = ps.executeQuery();
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();

        if (rs.next()){
            T t=clzz.newInstance();
            for (int i = 0; i < columnCount; i++) {
                Object cloumvalue = rs.getObject(i + 1);
                String columnLabel = rsmd.getColumnLabel(i + 1);

                Field field = clzz.getDeclaredField(columnLabel);
                field.setAccessible(true);
                field.set(t,cloumvalue);

            }
            return t;
        }
        closeResource(null,ps,rs);


        return null;
    }
    public static <T> List<T> selectlist(Connection connection,Class<T> clzz , String sql, Object ...args) throws Exception, InstantiationException {
        ArrayList<T> list=new ArrayList<>();

        PreparedStatement ps = connection.prepareStatement(sql);

        for (int i = 0; i < args.length; i++) {
            ps.setObject(i+1,args[i]);
        }

        ResultSet rs = ps.executeQuery();
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();

        while (rs.next()){
            T t=clzz.newInstance();
            for (int i = 0; i < columnCount; i++) {
                Object cloumvalue = rs.getObject(i + 1);
                String columnLabel = rsmd.getColumnLabel(i + 1);

                Field field = clzz.getDeclaredField(columnLabel);
                field.setAccessible(true);
                field.set(t,cloumvalue);

            }
            list.add(t);

        }

        closeResource(null,ps,rs);
        return list;



    }

    //关闭资源
    public static void closeResource(Connection conn){

        try {
            if(conn != null)
                conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void closeResource(Connection conn,Statement ps){
        try {
            if(ps != null)
                ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(conn != null)
                conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    /**
     *
     * @Description 关闭资源操作
     * @author shkstart
     * @date 上午10:21:15
     * @param conn
     * @param ps
     * @param rs
     */
    public static void closeResource(Connection conn, Statement ps, ResultSet rs){
        try {
            if(ps != null)
                ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(conn != null)
                conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(rs != null)
                rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
